package com.bcmusic.user.service.impl;

import com.bcmusic.user.dao.MusicianIdentificationMapper;
import com.bcmusic.user.pojo.MusicianIdentification;
import com.bcmusic.user.service.MusicianIdentificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:MusicianIdentification业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class MusicianIdentificationServiceImpl implements MusicianIdentificationService {

    @Autowired
    private MusicianIdentificationMapper musicianIdentificationMapper;


    /**
     * MusicianIdentification条件+分页查询
     * @param musicianIdentification 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<MusicianIdentification> findPage(MusicianIdentification musicianIdentification, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(musicianIdentification);
        //执行搜索
        return new PageInfo<MusicianIdentification>(musicianIdentificationMapper.selectByExample(example));
    }

    /**
     * MusicianIdentification分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<MusicianIdentification> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<MusicianIdentification>(musicianIdentificationMapper.selectAll());
    }

    /**
     * MusicianIdentification条件查询
     * @param musicianIdentification
     * @return
     */
    @Override
    public List<MusicianIdentification> findList(MusicianIdentification musicianIdentification){
        //构建查询条件
        Example example = createExample(musicianIdentification);
        //根据构建的条件查询数据
        return musicianIdentificationMapper.selectByExample(example);
    }


    /**
     * MusicianIdentification构建查询对象
     * @param musicianIdentification
     * @return
     */
    public Example createExample(MusicianIdentification musicianIdentification){
        Example example=new Example(MusicianIdentification.class);
        Example.Criteria criteria = example.createCriteria();
        if(musicianIdentification!=null){
            // 独立音乐人身份认证记录id。记录在提交身份认证/修改申请的时候创建。
            if(!StringUtils.isEmpty(musicianIdentification.getIdentificationId())){
                    criteria.andEqualTo("identificationId",musicianIdentification.getIdentificationId());
            }
            // 独立音乐人编号。关联musician表的musician_number字段。
            if(!StringUtils.isEmpty(musicianIdentification.getMusicianNumber())){
                    criteria.andEqualTo("musicianNumber",musicianIdentification.getMusicianNumber());
            }
            // 独立音乐人真实姓名。
            if(!StringUtils.isEmpty(musicianIdentification.getMusicianName())){
                    criteria.andEqualTo("musicianName",musicianIdentification.getMusicianName());
            }
            // 独立音乐人身份证号。香港等某些地区的身份证号长度在18位以内。
            if(!StringUtils.isEmpty(musicianIdentification.getIdCardNumber())){
                    criteria.andEqualTo("idCardNumber",musicianIdentification.getIdCardNumber());
            }
            // 独立音乐人银行卡号。银行卡号长度一般在13-19位，国际上也有12位的。
            if(!StringUtils.isEmpty(musicianIdentification.getBankCardId())){
                    criteria.andEqualTo("bankCardId",musicianIdentification.getBankCardId());
            }
            // 独立音乐人地址。
            if(!StringUtils.isEmpty(musicianIdentification.getMusicianLocation())){
                    criteria.andEqualTo("musicianLocation",musicianIdentification.getMusicianLocation());
            }
            // 独立音乐人邮箱。用来接收通知。
            if(!StringUtils.isEmpty(musicianIdentification.getMusicianMail())){
                    criteria.andEqualTo("musicianMail",musicianIdentification.getMusicianMail());
            }
            // 独立音乐人身份认证状态。0默认为未申请，1为待审核，2为已认证，-1为已驳回，-2为已废除。任意类型管理员修改独立音乐人的信息都默认为已认证状态。

            if(!StringUtils.isEmpty(musicianIdentification.getValidStatus())){
                    criteria.andEqualTo("validStatus",musicianIdentification.getValidStatus());
            }
            // 提交者编号。默认为提交认证/修改申请的独立音乐人或管理员编号，任意类型管理员修改独立音乐人信息时，申请者为该管理员。
            if(!StringUtils.isEmpty(musicianIdentification.getSubmitterNumber())){
                    criteria.andEqualTo("submitterNumber",musicianIdentification.getSubmitterNumber());
            }
            // 审核者编号。此处为任意类型的管理员。
            if(!StringUtils.isEmpty(musicianIdentification.getAuditorNumber())){
                    criteria.andEqualTo("auditorNumber",musicianIdentification.getAuditorNumber());
            }
            // 提交认证/修改申请时间。
            if(!StringUtils.isEmpty(musicianIdentification.getSubmitTime())){
                    criteria.andEqualTo("submitTime",musicianIdentification.getSubmitTime());
            }
            // 审核认证/修改申请时间。
            if(!StringUtils.isEmpty(musicianIdentification.getCheckTime())){
                    criteria.andEqualTo("checkTime",musicianIdentification.getCheckTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        musicianIdentificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改MusicianIdentification
     * @param musicianIdentification
     */
    @Override
    public void update(MusicianIdentification musicianIdentification){
        musicianIdentificationMapper.updateByPrimaryKey(musicianIdentification);
    }

    /**
     * 增加MusicianIdentification
     * @param musicianIdentification
     */
    @Override
    public MusicianIdentification add(MusicianIdentification musicianIdentification){
        musicianIdentificationMapper.insertSelective(musicianIdentification);
        return musicianIdentificationMapper.selectByPrimaryKey(musicianIdentification.getIdentificationId());
    }

    /**
     * 根据ID查询MusicianIdentification
     * @param id
     * @return
     */
    @Override
    public MusicianIdentification findById(Long id){
        return  musicianIdentificationMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询MusicianIdentification全部数据
     * @return
     */
    @Override
    public List<MusicianIdentification> findAll() {
        return musicianIdentificationMapper.selectAll();
    }

    @Override
    public MusicianIdentification updateSelective(MusicianIdentification musicianIdentification) {
        musicianIdentificationMapper.updateByPrimaryKeySelective(musicianIdentification);
        return musicianIdentificationMapper.selectByPrimaryKey(musicianIdentification.getIdentificationId());
    }
}
